package com.laicunba.service.activity.impl;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.laicunba.pojo.RegistryDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.pojo.vo.FinancingTargetBidVO;
import com.laicunba.service.activity.SummerAssaultService;
import com.laicunba.service.impl.BaseServiceImpl;

@Service
public class SummerAssaultServiceImpl extends BaseServiceImpl implements SummerAssaultService{

	@Override
	public Integer energyByBuy(String userId) {
		Integer energy = 0;
		Integer energyOne;
		BigDecimal buyAmount;
		int period;
		List<FinancingTargetBidVO> bidVOlist = queryDao.selectList("FINANCING_TARGET_BID.findBuyamountAndPeriod", userId);
		if(bidVOlist!=null && bidVOlist.size()>0){
			for(FinancingTargetBidVO bidVO:bidVOlist){
				buyAmount = bidVO.getBuyAmount();
				period = bidVO.getPeriod();
				if(period>=30&&period<60){
					energyOne = buyAmount.divide(BigDecimal.valueOf(200)).intValue();
					energy += energyOne;
				}else if(period>60&&period<=80){
					energyOne = buyAmount.divide(BigDecimal.valueOf(100)).intValue();
					energy += energyOne;
				}else if(period>80){
					energyOne = buyAmount.divide(BigDecimal.valueOf(50)).intValue();
					energy += energyOne;
				}
			}
		}
		return energy;
	}

	@Override
	public Integer energyByInvite(String inviteCode, String channel) {
		Integer energy = 0;
		UserDO userDO = new UserDO();
		userDO.setChannel(channel);
		userDO.setInviteCode(inviteCode);
		int count = queryDao.selectOne("USER.countByCodeChannel", userDO);
		if(count>=2){
			energy = 10;
		}else if(count== 1){
			energy = 5;
		}
		return energy;
	}

	@Override
	public RegistryDO registryDO(String userId) {
		RegistryDO registryDO = new RegistryDO();
		String key = "u."+userId+".summer-assault.activity";
		registryDO = queryDao.selectOne("REGISTRY.findByOne", key);
		return registryDO;
	}

	@Override
	public List<FinancingTargetBidVO> findListSummer() {
		List list = queryDao.selectList("FINANCING_TARGET_BID.findListSummer", null);
		return list;
	}

	@Override
	public Integer findCountSummer() {
		int number = queryDao.selectOne("FINANCING_TARGET_BID.findCountSummer", null);
		return number;
	}
}
